<?php
class Task_skuscache extends Minion_Task {

	//参数是否强制刷新
	protected $_options = array(
			'freash'=>NULL,
	);
	
	//缓存准备,计算所有的SKU
	public function _execute(array $params) {

		set_time_limit ( 0 );
		ini_set('memory_limit','256M');
		
		$echo = "";
		$echo .= "#start skus".date('Y-m-d h:i:s');
		//读取缓存,全部需要计算的sku
		$result = Cache::instance()->get('skuinfo_');
		//没有缓存，进行查找数据库
		if(empty($result) or isset($params['freash'])){
				
			$echo .= "#no cache".date('Y-m-d h:i:s');
			//sku在售,sku,oper
			$oper_flag = '';
			$sql = "select upper(products_sku) as sku,oper
			from erp_products_data
			where 1 $oper_flag  and products_remark_2 in (1,2,6,7,8) order by products_sku";
			$skus = DB::query(Database::SELECT, $sql)->execute('readonlyerp')->as_array();
					$oper = array();
					for($i=0; $i<count($skus);$i+=1000){
		
					//截取1000个根据sku过滤预警和日均》0sku,以及是否预警标志
				$skus_str = '';
				$sku1s = array_slice($skus,$i,1000);
				foreach($sku1s as $sku1){
				$skus_str .= $skus_str?'","'.$sku1['sku']:'"'.$sku1['sku'];
						$oper[$sku1['sku']]['oper'] = $sku1['oper'];//操作用户id
				}
		
				$skus_str = $skus_str.'"';
				$sql = "select sku,daysales,wave_days,target_time from product_attached where sku in(".$skus_str.") and warning_status=0 and daysales>0" ;
				$sku2s = DB::query(Database::SELECT, $sql)->execute('readonlyerp')->as_array();
							
				//sku daysales wave_days target_time
				$skus_info = array();
		
				foreach ($sku2s as $_sku2){
				$skus_info[$_sku2['sku']]['sku']		= $_sku2['sku'];
				$skus_info[$_sku2['sku']]['daysales']	= $_sku2['daysales'];
				$skus_info[$_sku2['sku']]['wave_days']	= $_sku2['wave_days'];
				$skus_info[$_sku2['sku']]['target_time']	= $_sku2['target_time'];
				}
					
						//通过供应商的状态过滤sku
				$solder_info = array();
				for($k=0;$k<count($sku2s);$k+=100){
				$sku2ss = array_slice($sku2s,$k,100);
				foreach($sku2ss as $sku2){
					
				//查询供应商
				$iprovider_ids_str = '';
				$sql ="select iproductID,iproviderID,delivery,price from erp_providerproducts where iproductID='".trim($sku2['sku'])."'";
						$iprovider_ids = DB::query(Database::SELECT, $sql)->execute('readonlyerp')->as_array();
								$count_temp = count($iprovider_ids);
		
								if($count_temp==0){
								continue;
				}
				elseif($count_temp==1){//只有一个供应商
								$iprovider_ids_str  .= $iprovider_ids_str?','.$iprovider_ids[0]['iproviderID']:$iprovider_ids[0]['iproviderID'];
								$solder_info[$iprovider_ids[0]['iproviderID']]['delivery'] = $iprovider_ids[0]['delivery'];
								$solder_info[$iprovider_ids[0]['iproviderID']]['price'] = $iprovider_ids[0]['price'];
								
								$sql = "select id from erp_provider where id =$iprovider_ids_str and state=2";
								$have = DB::query(Database::SELECT, $sql)->execute('readonlyerp')->current();
								
								if(isset($have['id'])){
										$skus_info[$sku2['sku']]['delivery'] = $solder_info[$have['id']]['delivery'];
										$skus_info[$sku2['sku']]['oper'] = $oper[$sku2['sku']]['oper'];
												$result[$sku2['sku']] = $skus_info[$sku2['sku']];
										}else{
										continue;
								}
				}
				elseif($count_temp>1){//有多个供应商
								foreach ($iprovider_ids as $iprovider_id){
								$iprovider_ids_str  .= $iprovider_ids_str?','.$iprovider_id['iproviderID']:$iprovider_id['iproviderID'];
								$solder_info[$iprovider_id['iproviderID']]['delivery'] = $iprovider_id['delivery'];
								$solder_info[$iprovider_id['iproviderID']]['price'] = $iprovider_id['price'];
							}
								$sql = "select id from erp_provider where id in (".$iprovider_ids_str.") and state=2 order by id";
								$have = DB::query(Database::SELECT, $sql)->execute('readonlyerp')->as_array();
							//比较供应商价格
								if(isset($have[0]['id'])){
								if(count($have)>1){
										$goal_id = 0;
										foreach ($have as $_have){
										$price_temp = 0;
										if($price_temp=0){
											$price_temp = $solder_info[$_have['id']]['price'];
											}elseif($price_temp > $solder_info[$_have['id']]['price']){
											$goal_id = $_have['id'];
											}else{}
											}
											$skus_info[$sku2['sku']]['delivery'] = $solder_info[$goal_id]['delivery'];
											$skus_info[$sku2['sku']]['oper'] = $oper[$sku2['sku']]['oper'];
										}else{
										$skus_info[$sku2['sku']]['delivery'] = $solder_info[$have[0]['id']]['delivery'];
										$skus_info[$sku2['sku']]['oper'] = $oper[$sku2['sku']]['oper'];
										}
										$result[$sku2['sku']] = $skus_info[$sku2['sku']];
										}
										}
										}
										}
			}
					//需要计算的计算全部的sku
					Cache::instance()->delete('skuinfo_');
					Cache::instance()->set('skuinfo_',$result,3600*12);
					$echo .= "#set skus cache".count($result);
					$echo .=  "#end".date('Y-m-d h:i:s');
						
		}
		echo $echo."\n";
// 		echo "#have skus cache ".count($result)."#".date('Y-m-d h:i:s')."\n";
	}
}